<?php 
/**
*file UserModel.class.php
*用户注册类
*
*@auther houtizong
*@version 1.0
*/
defined('ACC') ||exit ('ACC Denied');

class RegModel extends Model{
	protected $table = 'user'; 
	protected $pk = 'user_id'; //主键

    //表中的所有字段 跟$_POST过来的字段匹配
	protected $_field = array(
		'user_id','username','nick','password','email','regtime','lastlogin','salt'
		);
		
	//自动填充
	protected $_auto = array(
		    array('salt','function','salt'),
            array('regtime','function','time')
		);
   //验证
    protected $_valid = array(
            array('username',1,'用户名不能为空','require'),
            array('username',0,'用户名必须在4-16字符之间','length','4,16'),
            array('email',1,'邮箱不正确','email'),
            array('password',1,'密码不能为空','require')
       );

/**
*用户注册
*@param array $data 
*
*/
public function regUser($data){
	if ($data['password'] && $data['salt']) {
		$data['password'] = $this->md5Password($data['password'],$data['salt']);		
	}
	return $this->add($data);
}
/**
*对密码.salt加密 加强密码
*@param  string $password
*@param  string $salt
*return md5 ($password$salt)
*/
public function md5Password($password,$salt){
	return md5($password.$salt);
}

/**
*查询数据 检查用户名是否存在  验证用户名密码是否正确
*@param string  $username
*@param string md5 $password
*return int 行数  $password = ''的情况
*return  mixed
*/
public function checkUser($username,$password=''){
	if ($password == '') {
		$sql = 'select count(*) from '.$this->table.' where username="'.$username.'"';
		return $this->db->getOne($sql);
	}else {
		//查询数据
		$sql = 'select username,password,email,salt from '.$this->table.' where username="'.$username.'"';
	    $row =$this->db->getRow($sql);
	    //判断
	    if (empty($row)) {   //是否有值
	    	return false;
	    }
	    if ($row['password'] === $this->md5Password($password,$row['salt'])) { //密码错误
	    	unset($row['password']);
	        return $row; 
	    }else{
	    return false;
	}
	}	
}











}


?>